//
//  SCUtils.m
//  SuperCat
//
//  Created by 王杰 on 2019/3/26.
//  Copyright © 2019 和信金谷. All rights reserved.
//

#import "SCUtils.h"
#import "SCNavController.h"
#import "NSString+Helpers.h"
#import "AppDelegate.h"
#import "SCMainTabBarVC.h"
#import "MBProgressHUD.h"
#import "SC_MEASURE.h"
#import "SCDataHelper.h"

@implementation SCUtils

/// 获取当前页面
+ (SCBaseVC *)getCurrentVC {
    
    SCNavController *navController = [self getRootNavVC];
    
    SCBaseVC *vc = (SCBaseVC *) [navController.viewControllers lastObject];
    
    return vc;
    
}

/// 获取未销毁的指定界面
+ (SCBaseVC *)getCurrVC:(NSString *)className {
    
    if ([NSString isEmpty:className]) {
        return nil;
    }
    
    SCNavController *navVC = [self getRootNavVC];
    
    if (navVC == nil || navVC.viewControllers.count == 0) {
        return nil;
    }
    
    for (UIViewController *vc in navVC.viewControllers) {
        if ([vc isKindOfClass:NSClassFromString(className)]) {
            
            return (SCBaseVC *) vc;
        }
    }
    
    return nil;
    
}

+ (SCMainTabBarVC *)getRootVC {
    
    AppDelegate *app = (AppDelegate *) [UIApplication sharedApplication].delegate;
    
    UIWindow *window = app.window;
    
    SCMainTabBarVC *mainTabBarVC = (SCMainTabBarVC *) window.rootViewController;
    
    return mainTabBarVC;
    
}

/// 获得以BCNavController为跟视图的当前VC
+ (SCBaseVC *)getCurrentVCWithNavRoot{
    AppDelegate *app = (AppDelegate *) [UIApplication sharedApplication].delegate;
    UIViewController *rootVC = app.window.rootViewController;
    if ([rootVC isKindOfClass:[SCNavController class]]) {
        SCNavController *navRootVC = (SCNavController *)rootVC;
        SCBaseVC *vc = (SCBaseVC *) [navRootVC.viewControllers lastObject];
        return vc;
    }else {
        SCMainTabBarVC *mainTabBarVC = (SCMainTabBarVC *)rootVC;
        SCNavController *navRootVC = (SCNavController *)mainTabBarVC.viewControllers[mainTabBarVC.selectedIndex];
        SCBaseVC *vc = (SCBaseVC *) [navRootVC.viewControllers lastObject];
        return vc;
    }
}

+ (SCNavController *)getRootNavVC {
    
    SCMainTabBarVC *mainTabBarVC = (SCMainTabBarVC *) [self getRootVC];
    
    SCNavController *navController = mainTabBarVC.viewControllers[mainTabBarVC.selectedIndex];
    
    return navController;
}

+ (void)toast:(NSString *)toastStr {
    
    [self toast:toastStr delay:2];
}

+ (void)toast:(NSString *)toastStr delay:(CGFloat)delay{
    
    if ([NSString isEmpty:toastStr]) {
        return;
    }
    if ([toastStr isEqualToString:@"操作太频繁"]) {
        return;
    }
    //只显示文字
    MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:kScreenWindow];
    [kScreenWindow addSubview:hud];
    hud.label.text = toastStr;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.margin = 15;
    hud.contentColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    CGFloat yOffset = (kScheenHeight - kBtmSafeareaHeight)/2 - kBtmSafeareaHeight - kTabBarHeight - 40;
    hud.offset = CGPointMake(hud.offset.x, yOffset);
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:delay];
}

+ (void)showHUDToView:(UIView *)toView {
    
    [MBProgressHUD showHUDAddedTo:toView animated:YES];
    
}

+ (void)hideHUDFromView:(UIView *)view {
    
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (BOOL)isLogin{
    return [SCDataHelper getUserInfo] ? YES:NO;
}

+ (void)logout{
    [SCDataHelper saveUserInfo:nil];
    //TODO:跳转到登录页面
}

@end
